package com.yj.springbootstudy.algorithm.array;

/**
 * 使用数据完成增删改查
 */
public class MyArrayList {

    private int[] arr;
    private int size;
    private int capacity;
    private int front; //头指针
    private int rear;  //尾指针

    public MyArrayList(int capacity){
        this.capacity = capacity;
        this.arr = new int[capacity];
    }

    public boolean isFull() {
        if (size == capacity) {
            return true;
        }
        return false;
    }

    public boolean isEmpty() {
        if (arr.length == 0) {
            return true;
        }
        return false;
    }

    public void add(int data,int index) {
        if (isFull()){
            throw new RuntimeException("当前数组已满！");
        }
        for (int i = arr.length-1; i >= index ; i--) {
            arr[i+1] = arr[i];
        }
        arr[index] = data;
        size++;
    }

    public int remove(int index){
        int removeElement = arr[index];
        if (isEmpty()){
            throw new RuntimeException("当前队列为空！");
        }
        for (int i = index; i < arr.length-1 ; i++) {
            arr[i] = arr[i+1];
        }

        size--;
        arr[size] = 0;
        return removeElement;
    }


    public void list(){
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

    public static void main(String[] args) {
        MyArrayList list = new MyArrayList(5);
        list.add(1,0);
        list.add(2,1);
        list.add(3,2);
        list.add(4,3);
        list.add(5,4);
        list.list();
        list.remove(0);
        System.out.println("--------------");
        list.list();
    }
}
